using System;
using System.Collections.Generic;
using System.Text;

namespace Decav.Reality
{
    /// <summary>
    /// An address for electronic messages (email).
    /// </summary>
    [Serializable]
    public struct EmailAddress
    {
        public EmailAddress(string address)
        {
            if (address == null)
                throw new ArgumentNullException("address");

            if (address.Split('@').Length != 2)
                throw new ArgumentException("The mail address is not valid.");

            _address = address.ToLower();
        }

        private string _address;

        /// <summary>
        /// The empty value of an <see cref="EmailAddress"/>.
        /// </summary>
        public static readonly EmailAddress Empty = new EmailAddress();

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (obj.GetType() != typeof(EmailAddress))
                return false;

            EmailAddress addr = (EmailAddress)obj;

            return (addr._address == this._address);
        }

        public override int GetHashCode()
        {
            if (_address == null)
                return 0;

            return _address.GetHashCode();
        }

        public static bool operator ==(EmailAddress left, EmailAddress right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(EmailAddress left, EmailAddress right)
        {
            return !left.Equals(right);
        }

        /// <summary>
        /// Returns the email address.
        /// </summary>
        /// <returns>The email address as a string.</returns>
        public override string ToString()
        {
            if (_address == null)
                return string.Empty;

            return _address;
        }
    }
}
